package com.lsh.controller;

import com.lsh.domain.UserDatum;
import com.lsh.exception.SysException;
import com.lsh.service.HomeService;
import com.lsh.service.IndexService;
import com.lsh.util.DistrictUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 我的主页模块
 */
@Controller
@RequestMapping("/user")
public class HomeController {

    @Autowired
    IndexService indexService;

    @Autowired
    HomeService homeService;

    /**
     * 我的主页
     * @param mv
     * @return
     */
    @RequestMapping(value = "/home")
    public ModelAndView home(@CookieValue("TOKEN") String token, ModelAndView mv) throws SysException {
        String userId = indexService.findByUserId(token);
        if(userId == null)
            mv.setViewName("/index");
        else{
            Map map = homeService.showUser(Integer.parseInt(userId));
            UserDatum userDatum = (UserDatum) map.get("userDatum");
            mv.addObject("nickname", userDatum.getNickname());
            mv.addObject("pro", userDatum.getProvince());
            mv.addObject("cit", userDatum.getCity());
            mv.addObject("UserDatum", userDatum);
            mv.addObject("headPortrait", map.get("headPortrait"));
            mv.setViewName("/user/home");
        }
        return mv;
    }

    /**
     * 更新省市对应地址
     * @param province
     * @param mv
     * @return
     */
    @RequestMapping("/updateCity")
    public ModelAndView updateCity(@RequestParam String province, ModelAndView mv){
        Map<String, List<String>> map = DistrictUtil.findDistrictMap();
        List<String> list = map.get(province);
        mv.addObject("cityList", list);
        JSONObject jo = new JSONObject();
        jo.putAll(mv.getModel());
        mv.clear();
        mv.addObject("data", jo.toString());
        mv.setViewName("/JSON");
        return mv;
    }

    // 更改头像


    /**
     * 更改个人备注
     * @param token
     * @param data
     * @param mv
     * @return
     * @throws SysException
     */
    @RequestMapping(value = "/updateRemark", method = RequestMethod.POST)
    public ModelAndView updateRemark(@CookieValue("TOKEN") String token, @RequestBody String data, ModelAndView mv) throws SysException {
        String userId = indexService.findByUserId(token);
        if(userId == null) mv.setViewName("/index");
        else{
            JSONObject jo = new JSONObject();
            String remark = jo.fromObject(data).get("remark").toString();
            homeService.updateRemark(remark, userId);
            mv.addObject("SUCCESS", 1);
            jo.putAll(mv.getModel());
            mv.clear();
            mv.addObject("data", jo.toString());
            mv.setViewName("/JSON");
        }
        return mv;
    }

    /**
     * 更改个人信息
     * @param token
     * @param userDatum
     * @param mv
     * @return
     * @throws SysException
     */
    @RequestMapping(value = "/updateDatum", method = RequestMethod.POST)
    public ModelAndView updateDatum(@CookieValue("TOKEN") String token, UserDatum userDatum, ModelAndView mv) throws SysException {
        String userId = indexService.findByUserId(token);
        if(userId == null) mv.setViewName("/index");
        else{
            userDatum.setUid(Integer.parseInt(userId));
            homeService.updateUserDatum(userDatum);
            mv.addObject("SUCCESS", "修改成功");
            JSONObject jo = new JSONObject();
            jo.putAll(mv.getModel());
            mv.clear();
            mv.addObject("data", jo.toString());
            mv.setViewName("/JSON");
        }
        return mv;
    }

    /**
     * 上传头像
     * @param headImg
     * @param request
     * @param mv
     * @return
     */
    @RequestMapping(value = "/uploadHeaderImg", method = RequestMethod.POST)
    public ModelAndView uploadHeaderImg(@RequestParam("headerImg") String headImg, @CookieValue("TOKEN") String token,
                                        HttpServletRequest request, ModelAndView mv) throws SysException {
        int userId = Integer.parseInt(indexService.findByUserId(token)); // 获取用户ID
        String path = request.getSession().getServletContext().getRealPath("" ); // 获取项目图片根目录
        String contextPath = request.getContextPath(); // 获取项目名
        path = path.replace(contextPath, "");
        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        homeService.saveHeadPortrait(userId, path, url, headImg);
        mv.addObject("SUCCESS", "保存成功");
        JSONObject jo = new JSONObject();
        jo.putAll(mv.getModel());
        mv.clear();
        mv.addObject("data", jo.toString());
        mv.setViewName("/JSON");
        return mv;
    }
}
